//
//  CWPresentationController.swift
//  SinaBlog
//
//  Created by ChenWei on 15/10/6.
//  Copyright © 2015年 cw. All rights reserved.
//

import UIKit

class CWPresentationController: UIPresentationController {
    /// 转场界面的frame
    var presentedViewFrame = CGRectZero
    
    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
    }
    
    /// 自定义转场界面即将布局子控件时调用
    override func containerViewWillLayoutSubviews() {
        // 1.设置转场界面的frame
        presentedViewController.view.frame = presentedViewFrame
        
        // 2.添加遮盖
        containerView?.insertSubview(coverView, atIndex: 0)
    }
    
    // MARK: - 监听遮盖的点击
    @objc private func coverViewClick() {
        // 发通知
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
    
    // MARK: - 懒加载
    lazy var coverView: UIView = {
        let coverView = UIView()
        coverView.frame = UIScreen.mainScreen().bounds
        coverView.backgroundColor = UIColor(white: 0.2, alpha: 0.2)
        
        // 添加手势
        let tap = UITapGestureRecognizer(target: self, action: Selector("coverViewClick"))
        coverView.addGestureRecognizer(tap)
        
        return coverView
    }()
}


